<template>
  <el-card>
    <template #header>商品分类</template>

    <div>
      <!-- 1.0 添加分类的按钮 -->
      <p>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </p>
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%">
        <div>
          <!-- 添加分类的表单 -->
          <el-form
            :model="cateForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="分类名称" prop="cateName">
              <el-input v-model="cateForm.cateName"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch
                v-model="cateForm.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="addCate('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 2.0 渲染分类的表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="#" width="55"></el-table-column>
        <el-table-column label="分类名称">
          <template v-slot="{ row }">
            <el-input v-if="row.flag" v-model="row.cateName"></el-input>
            <span v-else>{{ row.cateName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="!row.flag"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'success' : 'primary'"
              size="mini"
              @click="checkoutState(row)"
              >{{ row.flag ? '完成' : '编辑' }} {{ row.flag }}</el-button
            >
            <el-button type="danger" size="mini" @click="delCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.0 分页的处理 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { getCate, addCategory, delCategory, editCategory } from '@/api/cate.js'

export default {
  data() {
    return {
      pageSize: 5,
      currentPage: 1,
      total: 0,
      tableData: [],
      dialogVisible: false,
      //  数据对象
      cateForm: {
        cateName: '',
        state: true
      },
      // 规则对象
      rules: {
        cateName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCates()
  },
  methods: {
    async getCates() {
      const { data, total } = await getCate({
        pageSize: this.pageSize,
        currentPage: this.currentPage
      })
      // console.log(data)
      this.tableData = data
      this.total = total

      // 给每一条数据添加一个flag
      this.tableData.forEach(item => {
        // item.flag = false
        // 要添加响应的数据，使用 $set
        this.$set(item, 'flag', false)
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getCates()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getCates()
    },
    addCate(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code } = await addCategory(this.cateForm)
          if (code === 0) {
            this.dialogVisible = false
            this.getCates()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    delCate(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 在这里发起请求
          const { code } = await delCategory({ id })
          // console.log(data)
          if (code === 0) {
            this.getCates()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async checkoutState(row) {
      if (row.flag) {
        // 发请求去修改
        const obj = { ...row }
        obj.state = Boolean(obj.state)
        const { code } = await editCategory(obj)
        if (code === 0) {
          this.getCates()
          row.flag = !row.flag
        }
      } else {
        row.flag = !row.flag
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
